<template>
  <div class="app-container">
      <el-tabs type="border-card" style="margin: 10px;">
          <el-tab-pane label="未处理">
              <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
                  <el-form-item label="投诉对象" prop="target">
                      <el-select v-model="queryParams.target" placeholder="请选择投诉对象" clearable size="small">
                          <el-option
                                  v-for="item in objOptions"
                                  :key="item.value"
                                  :label="item.label"
                                  :value="item.value"
                          />
                      </el-select>
                  </el-form-item>
                  <el-form-item label="期望时间" prop="serviceTime">
                      <el-date-picker
                              v-model="queryParams.serviceTime"
                              clearable
                              size="small"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择期望处理时间"
                      />
                  </el-form-item>
                  <el-form-item label="处理状态" prop="status">
                      <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable size="small">
                          <el-option
                                  v-for="item in dealOptions"
                                  :key="item.value"
                                  :label="item.label"
                                  :value="item.value"
                          />
                      </el-select>
                  </el-form-item>
                  <el-form-item label="是否介入" prop="leaderIntervene">
                      <el-select v-model="queryParams.leaderIntervene" placeholder="请选择" clearable size="small">
                          <el-option
                                  v-for="item in interveneOptions"
                                  :key="item.value"
                                  :label="item.label"
                                  :value="item.value"
                          />
                      </el-select>
                  </el-form-item>
                  <el-form-item label="修改时间" prop="modifyTime">
                      <el-date-picker
                              v-model="queryParams.modifyTime"
                              clearable
                              size="small"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择修改时间"
                      />
                  </el-form-item>
                  <el-form-item>
                      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                  </el-form-item>
              </el-form>

              <el-row :gutter="10" class="mb8">
                  <el-col :span="1.5">
                      <el-button
                              v-hasPermi="['community:complain:edit']"
                              type="success"
                              plain
                              icon="el-icon-edit"
                              size="mini"
                              :disabled="single"
                              @click="handleUpdate"
                      >修改</el-button>
                  </el-col>
                  <el-col :span="1.5">
                      <el-button
                              v-hasPermi="['community:complain:remove']"
                              type="danger"
                              plain
                              icon="el-icon-delete"
                              size="mini"
                              :disabled="multiple"
                              @click="handleDelete"
                      >删除</el-button>
                  </el-col>
                  <el-col :span="1.5">
                      <el-button
                              v-hasPermi="['community:complain:export']"
                              type="warning"
                              plain
                              icon="el-icon-download"
                              size="mini"
                              @click="handleExport"
                      >导出</el-button>
                  </el-col>
                  <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
              </el-row>
              <div style="display: flex;flex-direction:column; align-items: center;">
                  <el-table v-loading="loading" :data="dealList" @selection-change="handleSelectionChange">
                      <el-table-column type="selection" width="55" align="center" />
                      <el-table-column label="业主编号" align="center" prop="userId" />
                      <el-table-column label="投诉对象" align="center" prop="target">
                      </el-table-column>
                      <el-table-column label="投诉内容" align="center" prop="content" />
                      <el-table-column label="期望处理时间" align="center" prop="serviceTime" width="180">
                          <template slot-scope="scope">
                              <!--                          <span>{{ parseTime(scope.row.serviceTime, '{y}-{m}-{d}') }}</span>-->
                              <span>{{ scope.row.serviceTime}}</span>
                          </template>
                      </el-table-column>
                      <el-table-column label="处理状态" align="center" prop="status">
                      </el-table-column>
                      <el-table-column label="上级是否介入" align="center" prop="leaderIntervene">
                          <template slot-scope="scope">
                              <el-tag :type="scope.row.leaderIntervene === false ? 'info' : 'success' ">
                                  {{ scope.row.leaderIntervene === false ? '否' : '是' }}
                              </el-tag>
                          </template>
                      </el-table-column>
                      <el-table-column label="修改时间" align="center" prop="modifyTime" width="180">
                          <template slot-scope="scope">
                              <span>{{ parseTime(scope.row.modifyTime, '{y}-{m}-{d}') }}</span>
                          </template>
                      </el-table-column>
                      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                          <template slot-scope="scope">
                              <el-button
                                      v-hasPermi="['community:complain:edit']"
                                      size="mini"
                                      type="text"
                                      icon="el-icon-view"
                                      @click="handleShowDetail(scope.row)"
                              >详情</el-button>
                              <el-button
                                      v-hasPermi="['community:complain:edit']"
                                      size="mini"
                                      type="text"
                                      icon="el-icon-edit"
                                      @click="handleUpdate(scope.row)"
                              >修改</el-button>
                              <el-button
                                      v-hasPermi="['community:complain:remove']"
                                      size="mini"
                                      type="text"
                                      icon="el-icon-delete"
                                      @click="handleDelete(scope.row)"
                              >删除</el-button>
                          </template>
                      </el-table-column>
                  </el-table>
                  <pagination
                          v-show="dealTotal>0"
                          :total="dealTotal"
                          :page.sync="queryParams.pageNum"
                          :limit.sync="queryParams.pageSize"
                          @pagination="getList"/>
              </div>
          </el-tab-pane>
          <el-tab-pane label="已处理" style="width: 100%;">
              <div style="margin: 10px;">
                  <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
                      <el-form-item label="投诉对象" prop="target">
                          <el-select v-model="queryParams.target" placeholder="请选择投诉对象" clearable size="small">
                              <el-option
                                      v-for="item in objOptions"
                                      :key="item.value"
                                      :label="item.label"
                                      :value="item.value"
                              />
                          </el-select>
                      </el-form-item>
                      <el-form-item label="期望时间" prop="serviceTime">
                          <el-date-picker
                                  v-model="queryParams.serviceTime"
                                  clearable
                                  size="small"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  placeholder="选择期望处理时间"
                          />
                      </el-form-item>
                      <el-form-item label="是否介入" prop="leaderIntervene">
                          <el-select v-model="queryParams.leaderIntervene" placeholder="请选择" clearable size="small">
                              <el-option
                                      v-for="item in interveneOptions"
                                      :key="item.value"
                                      :label="item.label"
                                      :value="item.value"
                              />
                          </el-select>
                      </el-form-item>
                      <el-form-item label="修改时间" prop="modifyTime">
                          <el-date-picker
                                  v-model="queryParams.modifyTime"
                                  clearable
                                  size="small"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  placeholder="选择修改时间"
                          />
                      </el-form-item>
                      <el-form-item>
                          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                      </el-form-item>
                  </el-form>
              </div>
              <div style="display: flex;flex-direction:column; align-items: center;">
                  <el-table v-loading="loading" :data="completeList" @selection-change="handleSelectionChange">
                      <el-table-column type="selection" width="55" align="center" />
                      <el-table-column label="业主编号" align="center" prop="userId" />
                      <el-table-column label="投诉对象" align="center" prop="target">
                      </el-table-column>
                      <el-table-column label="投诉内容" align="center" prop="content" />
                      <el-table-column label="期望处理时间" align="center" prop="serviceTime" width="180">
                      </el-table-column>
                      <el-table-column label="处理状态" align="center" prop="status">
                      </el-table-column>
                      <el-table-column label="上级是否介入" align="center" prop="leaderIntervene">
                          <template slot-scope="scope">
                              <el-tag :type="scope.row.leaderIntervene === false ? 'info' : 'success' ">
                                  {{ scope.row.leaderIntervene === false ? '否' : '是' }}
                              </el-tag>
                          </template>
                      </el-table-column>
                      <el-table-column label="修改时间" align="center" prop="modifyTime" width="180">
                          <template slot-scope="scope">
                              <span>{{ parseTime(scope.row.modifyTime, '{y}-{m}-{d}') }}</span>
                          </template>
                      </el-table-column>
                      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                          <template slot-scope="scope">
                              <el-button
                                      v-hasPermi="['community:complain:edit']"
                                      size="mini"
                                      type="text"
                                      icon="el-icon-view"
                                      @click="handleShowDetail(scope.row)"
                              >详情</el-button>
                              <el-button
                                      v-hasPermi="['community:complain:remove']"
                                      size="mini"
                                      type="text"
                                      icon="el-icon-delete"
                                      @click="handleDelete(scope.row)"
                              >删除</el-button>
                          </template>
                      </el-table-column>
                  </el-table>
                  <pagination
                          v-show="completeTotal>0"
                          :total="completeTotal"
                          :page.sync="queryParams.pageNum"
                          :limit.sync="queryParams.pageSize"
                          @pagination="getList"/>
              </div>
          </el-tab-pane>
      </el-tabs>

    <!-- 添加或修改业主投诉对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="业主编号" prop="userId" v-show="title === '修改业主投诉' ? false : true">
          <el-input v-model="form.userId" placeholder="请输入业主编号" v-show="title === '修改业主投诉' ? false : true" :disabled="title === '修改业主投诉' ? false : true" />
        </el-form-item>
        <el-form-item label="投诉对象" prop="target"  v-show="title === '修改业主投诉' ? false : true" >
          <el-input v-model="form.target" placeholder="请输入投诉对象" :disabled="title === '修改业主投诉' ? false : true" />
        </el-form-item>
        <el-form-item label="投诉内容" v-show="title === '修改业主投诉' ? false : true" >
          <el-input type="textarea" v-model="form.content" :min-height="192" :disabled="title === '修改业主投诉' ? false : true" />
        </el-form-item>
        <el-form-item label="投诉处理时间" prop="serviceTime" v-show="title === '修改业主投诉' ? false : true">
          <el-date-picker
            :disabled="title === '修改业主投诉' ? false : true"
            v-model="form.serviceTime"
            clearable
            size="small"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择投诉处理时间"
          />
        </el-form-item>
        <el-form-item label="处理状态" prop="status">
            <el-select v-model="form.status" placeholder="请选择处理状态" clearable size="small" :disabled="title === '修改业主投诉' ? false : true" >
                <el-option
                        v-for="item in dealOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"/>
            </el-select>
        </el-form-item>
        <el-form-item label="上级是否介入" prop="leaderIntervene">
            <el-select v-model="form.leaderIntervene" placeholder="请选择" clearable size="small" :disabled="title === '修改业主投诉' ? false : true" >
                <el-option
                        v-for="item in interveneOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                />
            </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
      <!-- 统计图表 各个对象投诉率 -->
  </div>
</template>

<script>
import { listComplain, getComplain, delComplain, addComplain, updateComplain, exportComplain } from '@/api/complain/complain'

export default {
  name: 'Complain',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      dealTotal: 0,
      completeTotal: 0,
      // 业主投诉表格数据
      dealList: [],
      completeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        communityId: null,
        userId: null,
        target: null,
        content: null,
        serviceTime: null,
        status: null,
        leaderIntervene: null,
        logicDelete: null,
        modifyTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      objOptions: [
        {
          value: '业主',
          label: '业主'
        }, {
          value: '物业管理人员',
          label: '物业管理人员'
        }, {
          value: '小区事件',
          label: '小区事件'
        }
      ],
      dealOptions: [
        {
          value: '未处理',
          label: '未处理'
        }, {
          value: '处理中',
          label: '处理中'
        }, {
          value: '上级介入中',
          label: '上级介入中'
        }, {
          value: '处理完成',
          label: '处理完成'
        }
      ],
      interveneOptions: [
        {
          value: false,
          label: '否'
        }, {
          value: true,
          label: '是'
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询业主投诉列表 */
    getList() {
      this.loading = true
      listComplain(this.queryParams).then(response => {
        this.dealList = []
        this.completeList = []
        response.rows.forEach(item => {
          if (item.status === '处理完成') {
            this.completeList.push(item)
          } else {
            this.dealList.push(item)
          }
        })
        this.dealTotal = this.dealList.length
        this.completeTotal = this.completeList.length
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        communityId: null,
        userId: null,
        target: null,
        content: null,
        serviceTime: null,
        status: '0',
        leaderIntervene: null,
        logicDelete: null,
        createTime: null,
        modifyTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加业主投诉'
    },
    /** 详情按钮操作 */
    handleShowDetail(row) {
      this.reset()
      const id = row.id || this.ids
      getComplain(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '详细'
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getComplain(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改业主投诉'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateComplain(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addComplain(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除【' + row.content + '】项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delComplain(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有业主投诉数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportComplain(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }
  }
}
</script>
